/*
 * Indiana University Community Grid Computing Lab Software License,Version 1.1
 *
 * Copyright (c) 2002-2006 Community Grid Computing Lab, Indiana University.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *      "This product includes software developed by the Indiana University
 *       Community Grid Computing Lab (http://www.communitygrids.iu.edu/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Indiana Univeristy","Indiana Univeristy Pervasive Techonology
 *    Labs" and  "Indiana Univeristy Community Grid Computing Lab" must not be
 *    used to endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    http://www.communitygrids.iu.edu/.
 *
 * 5. Products derived from this software may not use "Indiana
 *    University" name nor may "Indiana University" appear in their name,
 *    without prior written permission of the Indiana University.
 *
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * INDIANA UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package cgl.narada.mgmt.common;

import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

/**
 * Stores information regarding the Default broker node and its configuration
 * Created on Jan 10, 2006
 *
 * @author Harshawardhan Gadgil (hgadgil@grids.ucs.indiana.edu)
 */

public class DefaultBrokerProperties {

	private static String BROKER_HOST;

	private static Properties BROKER_CONFIGURATION;

	private static Vector<String> protocols;

	private static Vector<String> BROKER_HOSTS_TABLE;

	static {
		BROKER_HOST = "www.gridservicelocator.org";
		BROKER_CONFIGURATION = new Properties();

		// Set all default values for properties

		// /*
		// * -----------------------------------------------------------------
		// * Messaging NODE Properties Set for Home Configuration
		// * -------------------------------------------------------------------
		// */
		//
		// BROKER_CONFIGURATION.setProperty("NIOTCPBrokerPort", "6000");
		// BROKER_CONFIGURATION.setProperty("TCPBrokerPort", "6001");
		// BROKER_CONFIGURATION.setProperty("UDPBrokerPort", "0");
		// BROKER_CONFIGURATION.setProperty("PTCPBrokerPort", "0");
		// BROKER_CONFIGURATION.setProperty("PTCPStreamNumber", "5");
		// BROKER_CONFIGURATION.setProperty("MulticastGroupHost",
		// "224.224.224.224");
		// BROKER_CONFIGURATION.setProperty("MulticastGroupPort", "0");
		// BROKER_CONFIGURATION.setProperty("PoolTCPBrokerPort", "0");
		// BROKER_CONFIGURATION.setProperty("HTTPSBrokerPort", "0");
		// BROKER_CONFIGURATION.setProperty("SSLBrokerPort", "0");
		// BROKER_CONFIGURATION.setProperty("HTTPBrokerPort", "0");
		//
		// BROKER_CONFIGURATION.setProperty("AssignedAddress", "true");
		//
		// BROKER_CONFIGURATION.setProperty("SupportRTP", "no");
		// BROKER_CONFIGURATION.setProperty("ConcurrentConnectionLimit",
		// "3000");
		// BROKER_CONFIGURATION.setProperty("Discriminator", "156.56.*");
		// BROKER_CONFIGURATION.setProperty("AboutThisBroker",
		// "CGL - Root Node, Indiana University, Bloomington, IN, U.S.A.");
		// BROKER_CONFIGURATION.setProperty("BDNList", "");
		// BROKER_CONFIGURATION
		// .setProperty("DiscoveryResponsePolicy",
		// "cgl.narada.discovery.broker.DefaultBrokerDiscoveryRequestResponsePolicy");
		// BROKER_CONFIGURATION.setProperty("VirtualBrokerNetwork",
		// "network-CGL-1");
		// BROKER_CONFIGURATION.setProperty("BrokerKeyStore",
		// "keystore/NBSecurityTest.keys");
		// BROKER_CONFIGURATION.setProperty("MAXBrokerDiscoRequests", "1000");
		//
		// BROKER_CONFIGURATION.setProperty("username", "test");
		// BROKER_CONFIGURATION.setProperty("password", "test");

		/*
		 * -----------------------------------------------------------------
		 * Messaging NODE Properties Set for CGL Configuration
		 * -------------------------------------------------------------------
		 */

		// BROKER_CONFIGURATION.setProperty("NIOTCPBrokerPort", "46000");
		// BROKER_CONFIGURATION.setProperty("TCPBrokerPort", "46001");
		// BROKER_CONFIGURATION.setProperty("UDPBrokerPort", "0");
		// BROKER_CONFIGURATION.setProperty("PTCPBrokerPort", "0");
		// BROKER_CONFIGURATION.setProperty("PTCPStreamNumber", "5");
		// BROKER_CONFIGURATION.setProperty("MulticastGroupHost",
		// "224.224.224.224");
		// BROKER_CONFIGURATION.setProperty("MulticastGroupPort", "0");
		// BROKER_CONFIGURATION.setProperty("PoolTCPBrokerPort", "0");
		// BROKER_CONFIGURATION.setProperty("HTTPSBrokerPort", "0");
		// BROKER_CONFIGURATION.setProperty("SSLBrokerPort", "0");
		// BROKER_CONFIGURATION.setProperty("HTTPBrokerPort", "0");
		// BROKER_CONFIGURATION.setProperty("UP2PBrokerPort", "4444");

		// NB 1.1.5 Default Configuration !
		BROKER_CONFIGURATION.setProperty("NIOTCPBrokerPort", "3045");
		BROKER_CONFIGURATION.setProperty("TCPBrokerPort", "5045");
		BROKER_CONFIGURATION.setProperty("UDPBrokerPort", "3045");
		BROKER_CONFIGURATION.setProperty("PTCPBrokerPort", "0");
		BROKER_CONFIGURATION.setProperty("PTCPStreamNumber", "5");
		BROKER_CONFIGURATION.setProperty("MulticastGroupHost",
				"224.224.224.224");
		BROKER_CONFIGURATION.setProperty("MulticastGroupPort", "0");
		BROKER_CONFIGURATION.setProperty("PoolTCPBrokerPort", "0");
		BROKER_CONFIGURATION.setProperty("HTTPSBrokerPort", "7045");
		BROKER_CONFIGURATION.setProperty("SSLBrokerPort", "8045");
		BROKER_CONFIGURATION.setProperty("HTTPBrokerPort", "9045");
		BROKER_CONFIGURATION.setProperty("UP2PBrokerPort", "4444");

		BROKER_CONFIGURATION.setProperty("AssignedAddress", "true");
		BROKER_CONFIGURATION.setProperty("NodeAddress", "1,1,1,1");
		BROKER_CONFIGURATION.setProperty("SupportRTP", "no");
		BROKER_CONFIGURATION.setProperty("ConcurrentConnectionLimit", "3000");
		BROKER_CONFIGURATION.setProperty("Discriminator", "156.56.*");
		BROKER_CONFIGURATION.setProperty("AboutThisBroker",
				"CGL - Root Node, Indiana University, Bloomington, IN, U.S.A.");
		BROKER_CONFIGURATION.setProperty("BDNList", "");
		BROKER_CONFIGURATION
				.setProperty("DiscoveryResponsePolicy",
						"cgl.narada.discovery.broker.DefaultBrokerDiscoveryRequestResponsePolicy");
		BROKER_CONFIGURATION.setProperty("VirtualBrokerNetwork",
				"network-CGL-1");
		BROKER_CONFIGURATION.setProperty("BrokerKeyStore",
				"keystore/NBSecurityTest.keys");
		BROKER_CONFIGURATION.setProperty("MAXBrokerDiscoRequests", "1000");

		BROKER_CONFIGURATION.setProperty("PeerID", "peer");
		BROKER_CONFIGURATION.setProperty("RelayServerHost",
				"www.webservicelocator.org");
		BROKER_CONFIGURATION.setProperty("RelayServerPort", "60055");

		BROKER_CONFIGURATION.setProperty("username", "test");
		BROKER_CONFIGURATION.setProperty("password", "test");
		// Set the prioritized list of protocols that will be tried in the given
		// order
		protocols = new Vector<String>();
		protocols.add("tcp");
		protocols.add("niotcp");
		protocols.add("http");

		protocols.add("udp");

		protocols.add("https");
		protocols.add("ssl");
		protocols.add("multicast");
		protocols.add("ptcp");

		BROKER_HOSTS_TABLE = new Vector<String>();
		BROKER_HOSTS_TABLE.add("www.webservicelocator.org");
		BROKER_HOSTS_TABLE.add("trex.ucs.indiana.edu");
		BROKER_HOSTS_TABLE.add("www.messageservicelocator.org");
		BROKER_HOSTS_TABLE.add("www.gridservicelocator.org");

		// BROKER_HOSTS_TABLE.add("www.messageservicelocator.org");
	}

	/**
	 * @return Returns the BROKER_CONFIGURATION.
	 */
	public static final Properties getBROKER_CONFIGURATION() {
		return BROKER_CONFIGURATION;
	}

	/**
	 * @return Returns the BROKER_HOST.
	 */
	public static final String getBROKER_HOST() {
		return BROKER_HOST;
	}

	/**
	 * Returns the i'th broker from a list of preconfigured brokers
	 *
	 * @param i
	 * @return
	 */
	public static final String getBROKER_HOST(int i) {
		return (String) BROKER_HOSTS_TABLE.elementAt(i);
	}

	/**
	 * Returns the number of preconfigured brokers
	 *
	 * @return number of preconfigured brokers
	 */
	public static final int getNumberOfPreConfiguredHosts() {
		return BROKER_HOSTS_TABLE.size();
	}

	/**
	 * Returns the default port for the specified protocol
	 *
	 * @param protocol -
	 *            the protocol in question
	 * @return The port number, if the protocol is valid, NULL otherwise
	 */
	public static final String getPortForProtocol(Properties props,
			String protocol) {

		if (protocol.equals("multicast"))
			return props.getProperty("MulticastGroupHost") + ":"
					+ props.getProperty("MulticastGroupPort");
		else
			return props.getProperty(protocol.toUpperCase() + "BrokerPort");
	}

	/**
	 * Returns the default port for the specified protocol
	 *
	 * @param protocol -
	 *            the protocol in question
	 * @return The port number, if the protocol is valid, NULL otherwise
	 */
	public static final String getPortForProtocol(String protocol) {

		if (protocol.equals("multicast"))
			return BROKER_CONFIGURATION.getProperty("MulticastGroupHost") + ":"
					+ BROKER_CONFIGURATION.getProperty("MulticastGroupPort");
		else
			return BROKER_CONFIGURATION.getProperty(protocol.toUpperCase()
					+ "BrokerPort");
	}

	public static Enumeration getPrioritizedProtocolList() {
		return protocols.elements();
	}

}
